package com.cskaoyan.market.controller.Admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.service.admin.MarketBrandService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/brand")
public class AdminBrandController {

    @Autowired
    MarketBrandService brandService;

    @RequiresPermissions("admin:brand:list")
    @RequiresPermissionsDesc(menu = {"品牌管理", "品牌列表"}, button = "显示")
    @GetMapping("list")
    public Object list(Integer page, Integer limit, Integer id, String name, String sort, String order) {
        List<MarketBrand> marketBrandList = brandService.list(page, limit, id, name, sort, order);
        //返回响应
        return ResponseUtil.okList(marketBrandList);
    }

    @RequiresPermissions("admin:brand:delete")
    @RequiresPermissionsDesc(menu = {"品牌管理", "品牌删除"}, button = "删除")
    @PostMapping("delete")
    public Object delete(@RequestBody MarketBrand marketBrand) {
        brandService.delete(marketBrand);
        // 可能需要加验证逻辑
        // 返回响应
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:brand:update")
    @RequiresPermissionsDesc(menu = {"品牌管理", "品牌修改"}, button = "修改")
    @PostMapping("update")
    public Object update(@RequestBody MarketBrand marketBrand) {
        // 获取请求参数
        // 将数据更新到数据库中
        brandService.update(marketBrand);
        // 返回响应
        Map<String, Object> data = new HashMap<>();
        data.put("id", marketBrand.getId());
        data.put("name", marketBrand.getName());
        data.put("desc", marketBrand.getDesc());
        data.put("picUrl", marketBrand.getPicUrl());
        data.put("floorPrice", marketBrand.getFloorPrice());
        data.put("addTime", marketBrand.getAddTime());
        data.put("updateTime", marketBrand.getUpdateTime());
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:brand:create")
    @RequiresPermissionsDesc(menu = {"品牌管理", "品牌创建"}, button = "添加")
    @PostMapping("create")
    public Object create(@RequestBody Map map) {
        // 获取请求参数
        String name = (String) map.get("name");
        String desc = (String) map.get("desc");
        String floorPrice = (String) map.get("floorPrice");
        String picUrl = (String) map.get("picUrl");
        //校验
        if (StringUtils.isEmpty(name) || name.isEmpty()) {
            return ResponseUtil.fail(404, "品牌商名称不能为空");
        }
        // 创建对象
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setDeleted(false);
        marketBrand.setName(name);
        marketBrand.setDesc(desc);
        marketBrand.setFloorPrice(new BigDecimal(floorPrice));
        marketBrand.setPicUrl(picUrl);
        marketBrand.setSortOrder((byte)50);
        marketBrand.setAddTime(LocalDateTime.now());
        marketBrand.setUpdateTime(LocalDateTime.now());
        //将对象添加到数据库中
        brandService.create(marketBrand);
        // 返回响应
        Map<String, Object> data = new HashMap<>();
        data.put("id", marketBrand.getId());
        data.put("name", marketBrand.getName());
        data.put("desc", marketBrand.getDesc());
        data.put("picUrl", marketBrand.getPicUrl());
        data.put("floorPrice", marketBrand.getFloorPrice());
        data.put("addTime", marketBrand.getAddTime());
        data.put("updateTime", marketBrand.getUpdateTime());
        return ResponseUtil.ok(data);
    }
}
